clear all
set more off

local out2="\outregsQJE\"
local SSA="\Data_and_Codes\"
cd "\workfiles"


use step001_QJE_2, clear

*** join SSA data
joinby firm_id time using "`SSA'CGR_2006q1_2019q1_clean_ONLYINPSADD.dta", unmatched(master)
tab _merge
drop _merge

joinby firm_id time using "`SSA'CGR_2006q1_2019q1_clean_ONLYINPSWAGE.dta", unmatched(master)
tab _merge
drop _merge

tsset firm_id time

*** define treatment as given inflation TIMES treatment status
gen Treat=treatment_old*infl

*** winsorize actual and expected price changes
winsor dpre, gen(dpre_w) p(0.02)
winsor dprez, gen(dprez_w) p(0.02)

local controls0=" l2.dprez_w  i.l2.sitimp5 i.l2.occtot i.l2.sitgen i.l2.promig i.l2.liquid "	

*** restrict the sample only to the period with treatment and control groups
keep if time>=208


gen EMP=1 if add<=99 & add~=. 
replace EMP=2 if add>99 & add<=299 & add~=. 
replace EMP=3 if add>=300 & add~=.  

gen EXPORT=1 	 if export==1 & export~=.
replace EXPORT=2 if export==2 & export~=.
replace EXPORT=3 if (export==3 | export==4) & export~=.

gen GEO=1 if (area==1 | area==2)
replace GEO=2 if area==3
replace GEO=3 if area==4

gen YP=f1.dpre_w

gen YL=f1.lnL-l.lnL
winsor YL, gen(YL_w2) p(0.02)
	
gen lnL_new=log(add_new)*100	
gen YLA=f1.lnL_new-l.lnL_new
winsor YLA, gen(YLA_w2) p(0.02)

	
gen lnwage=log(wagebill/add_new)*100
winsor lnwage, gen(lnwage_w) p(0.02)
gen YW=f1.lnwage_w-l.lnwage_w
winsor YW, gen(YW_w2) p(0.02)
	

* YG: WE NEED SIMILAR RESULTS FOR CREDIT UTILIZATION
	
***=============================================================================
*** 						Panel A: price response
***=============================================================================

foreach var2 in setton3 EMP EXPORT GEO {
	quiet foreach var in YLA_w2 YW  {
		noisily di "`var2': `var': pooled"
		ivreghdfe `var' `controls0' (l.it12_all = l.Treat) [aw=peso], ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
		if "`var'"=="`var2'" {	
			outreg2 using "`out2'Table05_`var2'.dta", replace dta dec(3) ctitle("`var': pooled") addstat("1st stage F stat", e(widstat))	
		}
		else {
			outreg2 using "`out2'Table05_`var2'.dta", append dta dec(3) ctitle("`var': pooled") addstat("1st stage F stat", e(widstat))	
		}

		forvalues var3=1(1)3 {
			noisily di "`var': `var2' : `var3'"
			ivreghdfe `var' `controls0' (l.it12_all = l.Treat) [aw=peso] if `var2'==`var3', ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			outreg2 using "`out2'Table05_`var2'.dta", append dta dec(3) ctitle("`var': `var2' : `var3'") addstat("1st stage F stat", e(widstat))	
		}
		
	}
}	  

